<template>
  <div class="header">
    <div class="avatar">
      <el-image src="https://test-1256689020.cos.ap-nanjing.myqcloud.com/images/qq5.jpg" lazy fit="cover" />
      <div class="name">{{ username }}</div>
    </div>
    <nav>
      <template v-for="item in routes">
        <router-link v-if="!item.hidden" :key="item.path" :to="item.path" style="margin-right: 25px" @click.native="changePage(item.path)">
          <i :class="item.meta.icon" style="margin-right: 5px"></i>
          <span>{{ item.meta.title }}</span>
        </router-link>
      </template>
    </nav>

    <div class="search">
      <el-input v-model="query" placeholder="输入关键字查询文章" style="margin-right: 10px"></el-input>
      <el-button type="primary" @click="search" :disabled="true">搜索</el-button>
    </div>
  </div>
</template>

<script>
import indexApi from '@/api/home'
export default {
  name: 'HeaderView',
  props: {
    username: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      query: '',
      disabled: true,
      routes: ''
    }
  },
  mounted() {
    this.routes = this.$router.options.routes
  },
  created() {
    this.changePage(this.$router.history.current.path)
  },
  methods: {
    changePage(path) {
      if (path === '/' || path === '/type') {
        this.disabled = false
      } else {
        this.disabled = true
      }
    },
    search() {
      indexApi.search({ key: this.query }).then(res => {
        console.log(res.data.data)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 60px;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  justify-content: center;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 3;

  .avatar {
    width: 140px;
    height: 40px;
    margin-right: 50px;

    .el-image {
      width: 40px;
      height: 100%;
      margin-right: 10px;
      float: left;
      border-radius: 50%;
      vertical-align: middle;
      max-width: 100%;
    }

    .name {
      line-height: 40px;
      font-weight: 600;
      color: #fff;
      float: left;
    }
  }

  nav {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;

    a {
      text-align: center;
      color: #ffffffb3;

      &.router-link-exact-active {
        color: #0bbd87;
      }
    }
  }

  .search {
    width: 250px;
    height: 68%;
    margin-left: 25px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
